package com.jiamao.goplay.ui.user.vm;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.bean.BaseRep;
import com.jiamao.goplay.bean.PlaceWantGoItem;
import com.jiamao.goplay.bean.res.ResPlaceWanGo;
import com.jiamao.goplay.bean.res.ResUserInfoDetail;
import com.jiamao.goplay.config.Config;
import com.jiamao.goplay.data.BaseSubscriber;
import com.jiamao.goplay.data.DataRepository;
import com.jiamao.goplay.util.GsonUtil;
import com.tamsiree.rxkit.view.RxToast;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import me.tatarka.bindingcollectionadapter2.OnItemBind;

public class UserWantGotoVM extends BaseViewModel<DataRepository> {

    public ObservableField<Integer> emptyVisble = new ObservableField<>(View.GONE);

    private int count;
    private int page = 1;
//封装一个界面发生改变的观察者
public UIChangeObservable uc = new UIChangeObservable();

public class UIChangeObservable {
    //密码开关观察者
    public SingleLiveEvent<Boolean> pSwitchEvent = new SingleLiveEvent<>();
    public SingleLiveEvent<Integer> refreshEvent = new SingleLiveEvent<>();
    public SingleLiveEvent<Integer> refreshNUms = new SingleLiveEvent<>();
}


    public UserWantGotoVM(@NonNull Application application, DataRepository repository) {
        super(application, repository);
        //从本地取得数据绑定到View层


    }


//    public UserWantGotoVM(@NonNull Application application, DataRepository repository) {
//        super(application, repository);
//        //从本地取得数据绑定到View层
//
//        for (int i=0;i<10;i++){
//            MultiItemViewModel item = new UserWantGotoItemVM(this,null);
//            item.multiItemType("image");
//            observableList.add(item) ;
//        }
//
//
//    }




    //给RecyclerView添加ObservableList
    public ObservableList<MultiItemViewModel> observableList = new ObservableArrayList<>();
    //RecyclerView多布局添加ItemBinding
    public ItemBinding<MultiItemViewModel> itemBinding = ItemBinding.of(new OnItemBind<MultiItemViewModel>() {
        @Override
        public void onItemBind(ItemBinding itemBinding, int position, MultiItemViewModel item) {
            //通过item的类型, 动态设置Item加载的布局
            String itemType = (String) item.getItemType();
                //设置右布局
                itemBinding.set(BR.viewModel, R.layout.user_wangoto_item_layout);
            // itemBinding.set(BR.viewModel, R.layout.msg_home_item_layout);

        }
    });



    public void getDataList(int i){
        if (i == 0 ){
            page = 0;
        }
        page++;
        BaseSubscriber subscription = new BaseSubscriber<BaseRep<ResPlaceWanGo>>() {

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                uc.refreshEvent.setValue(0);
                e.printStackTrace();
                RxToast.showToastShort("网络出现故障");
            }

            @Override
            public void onComplete() {

            }

            @Override
            public void onNext(BaseRep<ResPlaceWanGo> baseRep) {
                super.onNext(baseRep);
                uc.refreshEvent.setValue(0);
                // dismissDialog();
                // dialog.dismiss();
                //保存账号密码
                if (baseRep.isOk()){
                    if (i == 0){
                        observableList.clear();
                        if (baseRep.getData().getList().size() == 0 ){
                            emptyVisble.set(View.VISIBLE);
                        }
                        count = baseRep.getData().getTotal();
                        uc.refreshNUms.setValue( count);
                    }

                   // uc.refreshNUms.setValue( baseRep.getData().getTotal());
//                    uc.refreshNUms.setValue(baseRep.getData().);
                    for (int i=0;i<baseRep.getData().getList().size();i++){
                        PlaceWantGoItem itemData =  baseRep.getData().getList().get(i);
                        KLog.d("html itemData"+itemData);
                        MultiItemViewModel item = new UserWantGotoItemVM(UserWantGotoVM.this,itemData);
                        item.multiItemType("image");
                        observableList.add(item) ;
                    }
                }else {
                    RxToast.showToastShort(baseRep.getMsg());
//
                }
            }
        };

        model.getApi().getMeGoToWhereList(Config.PAGELIMT,page)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider())) // 请求与View周期同步
                .compose(RxUtils.schedulersTransformer())  // 线程调度
                .compose(RxUtils.exceptionTransformer())   // 网络错误的异常转换
                //    .doOnSubscribe(this) //请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {

                    }
                })
                .subscribe(subscription);



    }



    public void getOtherDataList(int i,String uid){
        if (i == 0 ){
            page = 0;
        }
        page++;
        BaseSubscriber subscription = new BaseSubscriber<BaseRep<ResPlaceWanGo>>() {

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                uc.refreshEvent.setValue(0);
                e.printStackTrace();
                RxToast.showToastShort("网络出现故障");
            }

            @Override
            public void onComplete() {

            }

            @Override
            public void onNext(BaseRep<ResPlaceWanGo> baseRep) {
                super.onNext(baseRep);
                uc.refreshEvent.setValue(0);
                // dismissDialog();
                // dialog.dismiss();
                //保存账号密码
                if (baseRep.isOk()){
                    if (i == 0){
                        observableList.clear();
                        count = baseRep.getData().getTotal();
                        uc.refreshNUms.setValue( count);
                        uc.refreshNUms.setValue(baseRep.getData().getTotal());
                    }

                    for (int i=0;i<baseRep.getData().getList().size();i++){
                        PlaceWantGoItem itemData =  baseRep.getData().getList().get(i);
                        KLog.d("html itemData"+itemData);
                        MultiItemViewModel item = new UserWantGotoItemVM(UserWantGotoVM.this,itemData);
                        item.multiItemType("image");
                        observableList.add(item) ;
                    }
                }else {
                    RxToast.showToastShort(baseRep.getMsg());
//
                }
            }
        };

        model.getApi().getOtherGoToWhereList(uid,Config.PAGELIMT,page)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider())) // 请求与View周期同步
                .compose(RxUtils.schedulersTransformer())  // 线程调度
                .compose(RxUtils.exceptionTransformer())   // 网络错误的异常转换
                //    .doOnSubscribe(this) //请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {

                    }
                })
                .subscribe(subscription);



    }


    public void removeCloection(String id,UserWantGotoItemVM data){
        //uc.pSwitchEvent.setValue();
        model.getApi().detailRemoveCloection(id)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider())) // 请求与View周期同步
                .compose(RxUtils.schedulersTransformer())  // 线程调度
                //   .compose(RxUtils.exceptionTransformer())   // 网络错误的异常转换
                //    .doOnSubscribe(this) //请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        //  uc.pSwitchEvent.setValue(1);
                        showDialog();
                        // dialog.show();
                    }
                })
                .subscribe(new BaseSubscriber<BaseRep>() {
                    @Override
                    public void onNext(BaseRep baseRep) {

                        super.onNext(baseRep);
                        dismissDialog();
                        if (baseRep.isOk()){
                            observableList.remove(data);
                            count --;
                            uc.refreshNUms.setValue(count);
                        }else {
                            RxToast.showToastShort(baseRep.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                        //   uc.pSwitchEvent.setValue(2);
                        e.printStackTrace();
                        RxToast.showToastShort("网络出现故障");
                    }

                    @Override
                    public void onComplete() {
                        //   dismissDialog();
                    }

                });

    }


}
